/*!
 * Copyright (C) 2004-2009 Giant Interective Group, Inc. All rights reserved.
 * 版权所有 2004-2009 上海巨人网络科技有限公司
 * 
 * version #version, #date by Fuchun
 * Depends on jQuery 1.3.x
 * Depends on giant.core 2.0.x
 * Depends on ui.component 2.0.x
 */

var College = window.College = function() {};

(function($) {

var divCollege = null, btnOtherCountry = null, ulCollegeList = null, divProvList = null,
    sourceText = null;

/**
 * 初始化学校选择器各控件。
 */
College.init = function() {
	divCollege = $('#div_colleges');
	btnOtherCountry = $('#btn_other_country');
	ulCollegeList = $('#ul_college_list');
	divProvList = $('#div_pro_list');
	if(divProvList.html().trim() == '') {
		GiantAjax.responseJson({
			crossdomain: true,
			url: '/commons/ajaxLoadProvinceList.do',
			success: function(jdata) {
				var buf = new StringBuilder();
				for(var i = 0, len = jdata.length; i < len; i++) {
					buf.append('<a href="javascript:void(0)" lang="')
					.append(jdata[i].proId).append('"');
					if(i == 0) {
						buf.append(' class="current"');
					}
					buf.append(' onclick="College.selectProvince(this);">');
					buf.append(jdata[i].proName).append('</a>');
				}
				divCollege.html(buf.toString());
			}
		});
	} else {
		divProvList.find('a:first').click();
	}
};

/**
 * 选择城市。
 */
College.selectProvince = function(o) {
	if(College.currentProvince) {
		College.currentProvince.removeClass('current');
	}
	College.currentProvince = $(o);
    College.currentProvince.addClass('current');
    if(College.ProvId)
	    College.ProvId.val(College.currentProvince.attr('lang'));
	var currProId = ulCollegeList.attr('lang');
	if(currProId == College.currentProvince.attr('lang'))
	    return;
	ulCollegeList.attr('lang', $(o).attr('lang'));
	GiantAjax.responseJson({
		crossdomain: true,
		url: '/commons/ajaxLoadColleges.do',
		params: { provId: $(o).attr('lang') },
		success: function(jdata) {
			var buf = new StringBuilder();
			for(var i = 0, len = jdata.length; i < len; i++) {
				buf.append('<li><a href="javascript:void(0)" lang="');
				buf.append(jdata[i].cid).append('"');
				buf.append(' onclick="College.selectCollege(this);">');
				buf.append(jdata[i].cname).append('</a></li>');
			}
			ulCollegeList.html(buf.toString());
		}
	});
};

/**
 * 选择学校。
 */
College.selectCollege = function(o) {
	College.close();
	College.ProvId.val($(College.currentProvince).attr('lang'));
	College.ColId.val($(o).attr('lang'));
	if(College.currentCollege) {
		College.currentCollege.removeClass('current');
	}
	College.currentCollege = $(o);
    College.currentCollege.addClass('current');
	sourceText.val(College.currentCollege.text());
	if(College.ColId.val() == College.DeptId.attr('lang')) {
	    return;
	}
	College.DeptId.attr('lang', College.ColId.val());
	var cmb = $.combobox({ id: College.DeptId.attr('id') });
	var first = College.DeptId.find('option:first');
	first = {text:first[0].text, value:first[0].value};
	cmb.remove();
	cmb.addItem(first);
	GiantAjax.responseJson({
		crossdomain: true,
		url: '/commons/ajaxLoadDepartments.do',
		params: { colId: College.ColId.val() },
		success: function(jdata) {
			var opts = [];
			for(var i = 0, len = jdata.length; i < len; i++) {
				opts[i] = {
					text: jdata[i].deptName,
					value: jdata[i].deptId
				};
			}
			cmb.addItem(opts);
		}
	});
};

College.show = function(o, sProvId, sColId, sDeptId, sDeptNameId) {
	giant.ui.buildGloomyBackground();
	if(sourceText == null) {
		sourceText = $(o);
	}
	if(!College.ProvId)
	    College.ProvId = $(sProvId);
	if(!College.ColId)
	    College.ColId = $(sColId);
	if(!College.DeptId)
	    College.DeptId = $(sDeptId);
	if(!College.DeptName)
	    College.DeptName = $(sDeptNameId);
	sourceText.blur();
	if(B.msie && B.version < 7)
	    giant.ui.buildGloomyBackground();
    divCollege.css('display', 'block');
    var w = parseInt(divCollege[0].offsetWidth || divCollege.css('width'));
    var cs = [giant.dom.clientWidth(), giant.dom.clientHeight()];
    var ss = [giant.dom.scrollLeft(), giant.dom.scrollTop()];
    pos = [(((cs[0] + ss[0]) / 2) - (w / 2)), ss[1] + 150];
    divCollege.css({left:pos[0] + 'px', top:pos[1] + 'px'});
};

/**
 * 关闭学校选择器。
 */
College.close = function() {
	giant.ui.removeGloomyBackground();
    divCollege.fadeOut('fast');
};

College.deptChange = function(o) {
    var sDeptName = o.options[o.selectedIndex].text;
    College.DeptName.val(sDeptName);
};

})(jQuery);